package gemini.castor.ui.client.page.content.home.register.distributorid;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.ErrorWidget;
import gemini.castor.ui.client.page.widget.TextFieldTextNumber;
import gemini.castor.ui.client.utils.UiConstants;

import java.util.Iterator;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.validation.client.InvalidConstraint;
import com.google.gwt.validation.client.interfaces.IValidator;

public class DistributorIDView extends Composite implements
		DistributorIDPresenter.Display {

	@UiTemplate("DistributorIDView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, DistributorIDView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private IValidator<DistributorIDObject> validator;
	private DistributorIDObject object = new DistributorIDObject();
	private I18nConstants constants;

	public DistributorIDView() {
		initWidget(binder.createAndBindUi(this));
		validator = GWT.create(DistributorIDObject.class);
		constants = GWT.create(I18nConstants.class);
	}

	@Override
	public boolean isValidate() {
		resetStyle();
		bind();
		boolean result = true;
		Set<InvalidConstraint<DistributorIDObject>> invalidConstraints = validator
				.validate(object);
		Iterator<InvalidConstraint<DistributorIDObject>> it = invalidConstraints
				.iterator();

		while (it.hasNext()) {
			if (result){
				result = false;
			}
			InvalidConstraint<DistributorIDObject> ic = it.next();
			showError(ic.getItemName(), ic.getMessage());
		}
		return result;
	}

	private void resetStyle() {
		tbxId.removeStyleDependentName("error");
		tbxPIN.removeStyleDependentName("error");
	}

	private void showError(String widgetName, String errorString) {
		if (widgetName.equals("distributorCode")) {
			tbxId.addStyleDependentName("error");
		}else if (widgetName.equals("distributorPIN")) {
			tbxPIN.addStyleDependentName("error");
		}
		cnwError.setVisible(true);	
	}

	private void bind() {
		object.setDistributorCode(tbxId.getValue());
		object.setDistributorPIN(tbxPIN.getValue());
	}

	@UiField
	ErrorWidget cnwError;


	@UiField
	TextFieldTextNumber tbxId;

	@UiField
	TextBox tbxPIN;

	@UiField
	Button btnSubmit;

	@UiField
	Button btnReset;

	@Override
	public void loadData() {
		tbxId.setMaxLength(UiConstants.MAX_LEN_DIS_ID);
		cnwError.setMessage(constants.emptyField());
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasClickHandlers getSubmitClickHandlers() {
		return btnSubmit;
	}

	@Override
	public HasClickHandlers getResetClickHandlers() {
		return btnReset;
	}

	@UiHandler("btnReset")
	void onReset(ClickEvent e) {
		reset();
	}
	
	private void reset() {
		resetValue();
		resetStyle();
		cnwError.setVisible(false);	
	}

	private void resetValue() {
		tbxId.setValue("", true);
		tbxPIN.setValue("", true);
	}
	
	@Override
	public DistributorIDObject getObject(){
		return object;
	}
}
